package com.ruoyi.web.controller.assets;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.BorrowLog;
import com.ruoyi.system.domain.OliveAssets;
import com.ruoyi.system.domain.OliveAssetsRepair;
import com.ruoyi.system.domain.RepairAudit;
import com.ruoyi.system.service.IOliveAssetsRepairService;
import com.ruoyi.system.service.IOliveAssetsService;
import com.ruoyi.system.service.IRepairAuditService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 资产报修管理Controller
 *
 * @author hzl
 * @date 2024-12-08
 */
@RestController
@RequestMapping("/assets/repair")
public class OliveAssetsRepairController extends BaseController
{
    @Autowired
    private IOliveAssetsRepairService oliveAssetsRepairService;
    @Autowired
    private IRepairAuditService repairAuditService;


    /**
     * 查询资产报修列表
     */
    @PreAuthorize("@ss.hasPermi('assets:repair:list')")
    @GetMapping("/list")
    public TableDataInfo list(OliveAssets oliveAssets)
    {
        startPage();
        List<OliveAssets> list = oliveAssetsRepairService.selectOliveAssetsList(oliveAssets);
        return getDataTable(list);
    }

    /**
     * 导出资产借还列表
     */
    @PreAuthorize("@ss.hasPermi('system:repair:export')")
    @Log(title = "资产报修", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OliveAssets oliveAssets)
    {
        List<OliveAssets> list = oliveAssetsRepairService.selectOliveAssetsList(oliveAssets);
        ExcelUtil<OliveAssets> util = new ExcelUtil<OliveAssets>(OliveAssets.class);
        util.exportExcel(response, list, "资产报修数据");
    }




    /**
     * 获取资产报修详细信息
     */
    @PreAuthorize("@ss.hasPermi('assets:repair:query')")
    @GetMapping(value = "/{assetId}")
    public AjaxResult getInfo(@PathVariable("assetId") String assetId)
    {
        return success(oliveAssetsRepairService.selectOliveAssetsByAssetId(assetId));
    }

    /**
     * 新增资产借还
     */
    @PreAuthorize("@ss.hasPermi('system:repair:add')")
    @Log(title = "资产报修", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OliveAssets oliveAssets)
    {
        return toAjax(oliveAssetsRepairService.insertOliveAssets(oliveAssets));
    }

    /**
     * 修改资产借还
     */
    @PreAuthorize("@ss.hasPermi('system:repair:edit')")
    @Log(title = "资产报修", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OliveAssets oliveAssets)
    {
        return toAjax(oliveAssetsRepairService.updateOliveAssets(oliveAssets));
    }

    /**
     * 删除资产借还
     */
    @PreAuthorize("@ss.hasPermi('system:repair:remove')")
    @Log(title = "资产报修", businessType = BusinessType.DELETE)
    @DeleteMapping("/{assetIds}")
    public AjaxResult remove(@PathVariable String[] assetIds)
    {
        return toAjax(oliveAssetsRepairService.deleteOliveAssetsByAssetIds(assetIds));
    }

    /**
     * 资产报修
     */
    @PreAuthorize("@ss.hasPermi('assets:repair:request')")
    @Log(title = "资产报修", businessType = BusinessType.OTHER)
    @PostMapping("/request")
    public AjaxResult requestBorrow(@RequestBody OliveAssets oliveAssets,@RequestParam("repairReason") String repairReason) {
        // 获取当前登录用户的ID
        Long borrowerId = Long.valueOf(getUserId());

        // 获取报修资产的ID
        String assetId = oliveAssets.getAssetId();
        // 创建一个OliveAssetsRepair对象
        OliveAssetsRepair oliveAssetsRepair = new OliveAssetsRepair();
        // 设置报修人的ID
        oliveAssetsRepair.setUserId(borrowerId);
        // 设置报修资产的ID
        oliveAssetsRepair.setAssetId(assetId);
        // 设置报修原因
        oliveAssetsRepair.setRepairApplyReason(repairReason);
        // 设置设备状态为“待审核”
        oliveAssetsRepair.setStatus("待审核");
        oliveAssets.setAssetStatus(5L);

        // 设置默认审核状态为0
//        borrowLog.setAuditStatus(0);
        // 设置当前时间为借用时间
//        borrowLog.setBorrowTime(new Date());
        // 设置借用位置
//        borrowLog.setBorrowLocation("["+ oliveAssets.getAssetLatitude() + "," + oliveAssets.getAssetLongitude() + "]");
        // 插入报修列表
        oliveAssetsRepairService.insertOliveAssetsRepair(oliveAssetsRepair);
        return toAjax(oliveAssetsRepairService.updateOliveAssets(oliveAssets));
    }

    /**
     * 通过报修请求
     */
    @PreAuthorize("@ss.hasPermi('assets:repair:approve')")
    @Log(title = "资产报修", businessType = BusinessType.UPDATE)
    @PostMapping("/approve/{assetId}")
    public AjaxResult approveRepair(@RequestBody RepairAudit repairAudit) {

        String assetId = repairAudit.getAssetId();
        // 验证assetId是否为空
        if (assetId == null || assetId.isEmpty()) {
            return AjaxResult.error("资产ID不能为空");
        }
        System.out.println("assetId: " + assetId);
        OliveAssets asset = new OliveAssets();
        // 设置设备状态为“报修中”
        asset.setAssetId(assetId);
        asset.setAssetStatus(2L);
        // 更新资产状态
        int updateResult = oliveAssetsRepairService.updateOliveAssets(asset);
        if (updateResult > 0) {
            // 更新审核状态
            repairAudit.setStatus("通过"); // 假设1表示审核通过
            // 设置维修结果为"待维修"
            repairAudit.setRepairResult("待维修");
            repairAudit.setReviewTime(new Date()); // 更新审核时间
            // 更新审核人
            repairAudit.setAdminId(String.valueOf(getUserId()));

            repairAuditService.updateRepairAudit(repairAudit);
            return AjaxResult.success("审核通过");
        } else {
            return AjaxResult.error("更新资产状态失败");
        }

    }

    /**
     * 拒绝报修请求
     */
    @PreAuthorize("@ss.hasPermi('assets:repair:reject')")
    @Log(title = "资产报修", businessType = BusinessType.UPDATE)
    @PostMapping("/reject/{assetId}")
    public AjaxResult rejectBorrow(@RequestBody RepairAudit repairAudit) {

        String assetId = repairAudit.getAssetId();
        // 验证assetId是否为空
        if (assetId == null || assetId.isEmpty()) {
            return AjaxResult.error("资产ID不能为空");
        }
        System.out.println("assetId: " + assetId);
        OliveAssets asset = new OliveAssets();
        // 设置设备状态为“使用中”
        asset.setAssetId(assetId);
        asset.setAssetStatus(0L);
        // 更新资产状态
        int updateResult = oliveAssetsRepairService.updateOliveAssets(asset);
        if (updateResult > 0) {
            // 更新审核状态
            repairAudit.setStatus("拒绝"); // 1表示已审核
            // 更新审核结果
            repairAudit.setReviewTime(new Date()); // 更新审核时间
            repairAudit.setAdminId(String.valueOf(getUserId()));
            repairAuditService.updateRepairAudit(repairAudit);
            return AjaxResult.success("审核未通过");
        } else {
            return AjaxResult.error("更新资产状态失败");
        }
    }

    /**
     * 开始维修
     */
    @PreAuthorize("@ss.hasPermi('assets:repair:start')")
    @Log(title = "资产报修", businessType = BusinessType.UPDATE)
    @PostMapping("/start/{assetId}")
    public AjaxResult startRepair(@RequestBody RepairAudit repairAudit) {
        // 获取当前登录用户的ID
        Long repairPersonId = Long.valueOf(getUserId());
        String assetId = repairAudit.getAssetId();
        // 验证assetId是否为空
        if (assetId == null || assetId.isEmpty()) {
            return AjaxResult.error("资产ID不能为空");
        }
        System.out.println("assetId: " + assetId);

        OliveAssets asset = new OliveAssets();
        // 设置设备状态为“报修中”
        asset.setAssetId(assetId);
        asset.setAssetStatus(3L);
        // 更新资产状态
        int updateResult = oliveAssetsRepairService.updateOliveAssets(asset);

        if (updateResult > 0) {
            // 设置维修人员ID
            repairAudit.setRepairPerson(repairPersonId);
            // 设置维修开始时间
            repairAudit.setRepairTime(new Date());
            // 设置维修结果为“维修中”
            repairAudit.setRepairResult("维修中");
            // 更新报修记录
            repairAuditService.updateRepairAudit(repairAudit);
            return AjaxResult.success("开始维修成功");
        } else {
            return AjaxResult.error("开始维修失败");
        }

    }

    /**
     * 完成维修
     */
    @PreAuthorize("@ss.hasPermi('assets:repair:finish')")
    @Log(title = "资产报修", businessType = BusinessType.UPDATE)
    @PostMapping("/finish/{assetId}")
    public AjaxResult finishRepair(@RequestBody RepairAudit repairAudit) {

        String assetId = repairAudit.getAssetId();
        // 验证assetId是否为空
        if (assetId == null || assetId.isEmpty()) {
            return AjaxResult.error("资产ID不能为空");
        }
        System.out.println("assetId: " + assetId);
        OliveAssets asset = new OliveAssets();
        // 设置设备状态为“空闲中”
        asset.setAssetId(assetId);
        asset.setAssetStatus(0L);
        // 更新资产状态
        int updateResult = oliveAssetsRepairService.updateOliveAssets(asset);

        if (updateResult > 0) {

            // 设置维修结果为“已修复”
            repairAudit.setRepairResult(repairAudit.getRepairResult());
            // 更新报修记录
            repairAuditService.updateRepairAudit(repairAudit);
            return AjaxResult.success("结束维修成功");
        } else {
            return AjaxResult.error("结束维修失败");
        }

    }




}
